import 'package:PollOnLine/utils/utils.dart';
import 'package:bruno/bruno.dart';
import 'package:device_preview/device_preview.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'ui/welcome/welcome_view.dart';
import 'values/MyColors.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized(); //确保初始化
  // runApp(DevicePreview(
  //   enabled: !kReleaseMode,
  //   builder: (context) => const MyApp(), // Wrap your app
  // ));
  runApp(const MyApp());
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    // //设置状态栏的背景颜色
    statusBarColor: MyColors.FF1060DD,
    //状态栏的文字的颜色
    statusBarIconBrightness: Brightness.light,
  ));
}

final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return isPad(context)
        // iPad 布局
        ? ScreenUtilInit(
            designSize: const Size(417, 597),
            child: GetMaterialApp(
              title: '面访调查',
              localizationsDelegates: const [
                // pull_to_refresh 库 语言初始化
                RefreshLocalizations.delegate,
                // 为Material Components库提供了本地化的字符串和其他值pickMultiImage
                GlobalMaterialLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                // 定义widget默认的文本方向，从左到右或从右到左。
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: const [
                Locale('zh', 'CN'), // 中国
                Locale('en', 'US'), // English
              ],
              theme: ThemeData(
                // 修改主题的颜色
                primarySwatch: Colors.blue,
                // 设置视觉密度：适应平台密度
                visualDensity: VisualDensity.adaptivePlatformDensity,
                // 所有M3组件都配置了主题的默认颜色
                colorScheme: ColorScheme.fromSeed(seedColor: MyColors.FF1060DD),
                //  3.10开启 Material3库
                useMaterial3: false,
              ),
              navigatorKey: navigatorKey,
              builder: (BuildContext context, Widget? child) {
                // 屏幕适配初始化
                ScreenUtil.registerToBuild(context);
                // bruno主题配置
                BrnInitializer.register(allThemeConfig: XxxConfigUtils.defaultAllConfig);
                // 初始化 loading
                final easyLoading = EasyLoading.init();
                child = MediaQuery(
                  //设置文字大小不随系统设置改变
                  data: MediaQuery.of(context).copyWith(textScaler: TextScaler.noScaling),
                  //全局设置键盘收缩
                  child: GestureDetector(
                      onTap: () {
                        // 空白处取消TextField焦点
                        FocusScopeNode currentFocus = FocusScope.of(context);
                        if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
                          FocusManager.instance.primaryFocus?.unfocus();
                        }
                      },
                      child: child),
                );
                return easyLoading(context, child);
              },
              home: const WelcomePage(title: '欢迎页面'),
            ),
          )
        : ScreenUtilInit(
            designSize: const Size(375, 679),
            child: GetMaterialApp(
              title: '面访调查',
              localizationsDelegates: const [
                // pull_to_refresh 库 语言初始化
                RefreshLocalizations.delegate,
                // 为Material Components库提供了本地化的字符串和其他值pickMultiImage
                GlobalMaterialLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                // 定义widget默认的文本方向，从左到右或从右到左。
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: const [
                Locale('zh', 'CN'), // 中国
                Locale('en', 'US'), // English
              ],
              theme: ThemeData(
                // 修改主题的颜色
                primarySwatch: Colors.blue,
                // 设置视觉密度：适应平台密度
                visualDensity: VisualDensity.adaptivePlatformDensity,
                // 所有M3组件都配置了主题的默认颜色
                colorScheme: ColorScheme.fromSeed(seedColor: MyColors.FF1060DD),
                //  3.10开启 Material3库
                useMaterial3: false,
              ),
              navigatorKey: navigatorKey,
              builder: (BuildContext context, Widget? child) {
                // 屏幕适配初始化
                ScreenUtil.registerToBuild(context);
                // bruno主题配置
                BrnInitializer.register(allThemeConfig: XxxConfigUtils.defaultAllConfig);
                // 初始化 loading
                final easyLoading = EasyLoading.init();
                child = MediaQuery(
                  //设置文字大小不随系统设置改变
                  data: MediaQuery.of(context).copyWith(textScaler: TextScaler.noScaling),
                  //全局设置键盘收缩
                  child: GestureDetector(
                      onTap: () {
                        // 空白处取消TextField焦点
                        FocusScopeNode currentFocus = FocusScope.of(context);
                        if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
                          FocusManager.instance.primaryFocus?.unfocus();
                        }
                      },
                      child: child),
                );
                return easyLoading(context, child);
              },
              home: const WelcomePage(title: '欢迎页面'),
            ),
          );
  }
}

/// bruno主题配置
class XxxConfigUtils {
  static BrnAllThemeConfig defaultAllConfig = BrnAllThemeConfig(
    commonConfig: defaultCommonConfig,
    dialogConfig: defaultDialogConfig,
    // appBarConfig: defaultAppBarConfig,
    buttonConfig: defaultBrnButtonConfig,
  );

  /// 全局配置
  static BrnCommonConfig defaultCommonConfig = BrnCommonConfig(
    ///品牌色
    brandPrimary: MyColors.FF1060DD,
  );

  /// Dialog配置
  static BrnDialogConfig defaultDialogConfig = BrnDialogConfig(
    radius: 6.r,
  );

  /// button配置
  static BrnButtonConfig defaultBrnButtonConfig = BrnButtonConfig(
    bigButtonFontSize: 12.r,
    bigButtonRadius: 4.r,
    smallButtonFontSize: 12.r,
    smallButtonRadius: 4.r,
  );

  /// AppBar配置
  static BrnAppBarConfig defaultAppBarConfig = BrnAppBarConfig(
    backgroundColor: MyColors.FF1060DD,
    appBarHeight: 44.r,
    itemSpacing: 20.r,
    titleStyle: BrnTextStyle(
      color: Colors.white,
      fontSize: 16.r,
    ),
    //状态栏的文字的颜色
    systemUiOverlayStyle: const SystemUiOverlayStyle(
      statusBarIconBrightness: Brightness.light,
    ),
    leadIconBuilder: () => const Icon(Icons.arrow_back_ios, size: 16, color: Colors.white),
  );

  static BrnAppBarConfig defaultIpadAppBarConfig = BrnAppBarConfig(
    backgroundColor: MyColors.FF1060DD,
    appBarHeight: 40.r,
    itemSpacing: 20.r,
    titleStyle: BrnTextStyle(
      color: Colors.white,
      fontSize: 16.r,
    ),
    //状态栏的文字的颜色
    systemUiOverlayStyle: const SystemUiOverlayStyle(
      statusBarIconBrightness: Brightness.light,
    ),
    leadIconBuilder: () => Icon(Icons.arrow_back_ios, size: 16.r, color: Colors.white),
  );
}
